<script setup>
import { ref, toRefs, defineExpose } from "vue";
import { useDrawerStore } from "@/store/drawerDataStores.js";
import { message } from "@/utils/message";

const drawerStore = useDrawerStore();
const props = defineProps({
  devVarTableSelectedItem: {
    type: Object,
    default() {
      return {};
    }
  },
  publicForm: {
    type: Object,
    default() {
      return {};
    }
  }
});
const { devVarTableSelectedItem, publicForm } = toRefs(props);
const emit = defineEmits(["childFormObj"]);

const ruleFormRef = ref(),
  ruleForm = ref({

    targetPort: "8986112321004902017", // 目标端口

    clientId: "869701073946009", //clientId
    subscribeTopic: "460113466640674", //订阅主题

  });

// 配置按钮
const configClick = () => {
  if (
    ruleForm.value.targetPort &&

    ruleForm.value.subscribeTopic &&
    ruleForm.value.clientId

  ) {
    let MQTTObj = {
      broker_port: ruleForm.value.targetPort,
      SUB: ruleForm.value.subscribeTopic,
      ClientID: ruleForm.value.clientId,
    };

    let SQLObj = {
      ...ruleForm.value
    };
    let formObj = {
      MQTTObj: MQTTObj,
      SQLObj: SQLObj
    };
    emit("childFormObj", formObj);
  } else {
    message("请填写所有的数据", { type: "warning" });
  }
};
// const configClick = () => {
//   ruleFormRef.value.validate(valid => {
//     if (valid) {
//       emit("childFormObj", ruleForm.value);
//     } else {
//       return false;
//     }
//   });
// };
// 父组件调用清空函数
const clearVar = () => {
  const storeForm = { ...drawerStore.mqttForm };
  ruleFormRef.value.resetFields();
  drawerStore.mqttForm = { ...storeForm };
};
// 父组件调用保存函数
const saveStore = () => {
  drawerStore.mqttForm = { ...ruleForm.value };
};
// 父组件调用赋值函数
const copyStore = () => {
  if (devVarTableSelectedItem.value.state === 1) {
    ruleForm.value = {
      targetAddress: devVarTableSelectedItem.value.targetAddress, // 目标地址
      targetPort: devVarTableSelectedItem.value.targetPort, // 目标端口
      deviceAccount: devVarTableSelectedItem.value.deviceAccount, // 设备账户
      devicePassword: devVarTableSelectedItem.value.devicePassword, //设备密码
      clientId: devVarTableSelectedItem.value.clientId, //clientId
      subscribeTopic: devVarTableSelectedItem.value.subscribeTopic, //订阅主题
      releaseTopic: devVarTableSelectedItem.value.releaseTopic, //发布主题
      qos: devVarTableSelectedItem.value.qos, //qos
      clearSession: devVarTableSelectedItem.value.clearSession //清理会话状态
    };
  } else {
    ruleForm.value = drawerStore.mqttForm;
  }
};
defineExpose({
  clearVar,
  saveStore,
  copyStore
});
</script>

<template>
  <div class="form-main">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :inline="true"
      label-position="top"
    >

      <el-form-item
        label="ICCID"
        prop="targetPort"
        :rules="{ required: true, message: '请输入ICCID', trigger: 'blur' }"
      >
        <el-input v-model="ruleForm.targetPort" placeholder="请输入ICCID" />
      </el-form-item>


      <el-form-item
        label="IMEI"
        prop="clientId"
        :rules="{ required: true, message: '请输入IMEI', trigger: 'blur' }"
      >
        <el-input v-model="ruleForm.clientId" placeholder="请输入IMEI" />
      </el-form-item>
      <el-form-item
        label="IMSI"
        prop="subscribeTopic"
        :rules="{ required: true, message: '请输入IMSI', trigger: 'blur' }"
      >
        <el-input
          v-model="ruleForm.subscribeTopic"
          placeholder="请输入IMSI"
        />
      </el-form-item>
      <!-- <el-form-item
        label="发布主题"
        prop="releaseTopic"
        :rules="{ required: true, message: '请输入发布主题', trigger: 'blur' }"
      >
        <el-input
          v-model="ruleForm.releaseTopic"
          placeholder="请输入发布主题"
        />
      </el-form-item> -->

      <!-- <el-form-item
        label="清理会话状态"
        prop="clearSession"
        style="width: 192px"
        :rules="{ required: true, message: '请选择会话状态', trigger: 'blur' }"
      >
        <el-select v-model="ruleForm.clearSession" placeholder="请选择会话状态">
          <el-option label="0" value="0" />
          <el-option label="1" value="1" />
          <el-option label="2" value="2" />
        </el-select>
      </el-form-item> -->
    </el-form>
    <div class="config-btn">
      <el-button type="primary" @click="configClick">配置</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.form-btn {
  margin-top: 30px;
}

.config-btn {
  text-align: right;
  padding-top: 20px;
}
</style>
